//
//  JKAdviceViewController.swift
//  afanti
//
//  Created by albert on 16/5/7.
//  Copyright © 2016年 albert. All rights reserved.
//

import UIKit

class JKAdviceViewController: UIViewController {
    
    @IBOutlet weak var commitDeviceBtn: UIButton!
    
    @IBOutlet weak var textView: UITextView!
    
    init(){
        super.init(nibName: "JKAdviceViewController", bundle: nil)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    @IBAction func commitDeviceBtnClick(sender: UIButton) {
        view.endEditing(true)
        
        let time: NSTimeInterval = 0.5
        let delay = dispatch_time(DISPATCH_TIME_NOW,
            Int64(time * Double(NSEC_PER_SEC)))
        dispatch_after(delay, dispatch_get_main_queue()) {
            if #available(iOS 8.0, *) {
                self.navigationController?.popViewControllerAnimated(true)
            } else {
                // Fallback on earlier versions
            }
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        textView.delegate = self
        
        view.addGestureRecognizer(UITapGestureRecognizer.init(target: self, action: "viewTap"))
    }
    
    //MARK: - 点击退出键盘
    func viewTap(){
        view.endEditing(true)
    }
}

extension JKAdviceViewController: UITextViewDelegate {
    func textViewDidChange(textView: UITextView) {
        commitDeviceBtn.enabled = textView.hasText()
    }
}